<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="参数名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="addagent()">添加经纪人</el-button>

        <el-button
          v-if="isAuth('proprietary:agentuser:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
        <el-button
          v-if="isAuth('proprietary:agentuser:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="agentId"
        header-align="center"
        align="center"
        label="经纪人id"
      >
      </el-table-column>
      <el-table-column
        prop="agentName"
        header-align="center"
        align="center"
        label="经纪人名字"
      >
      </el-table-column>
      <el-table-column
        prop="agentPhone"
        header-align="center"
        align="center"
        label="经纪人电话"
      >
      </el-table-column>
      <el-table-column
        prop="agentCompany"
        header-align="center"
        align="center"
        label="公司id"
      >
      </el-table-column>
      <el-table-column
        prop="agentNewhouse"
        header-align="center"
        align="center"
        label="新房"
      >
      </el-table-column>
      <el-table-column
        prop="agentOldhouse"
        header-align="center"
        align="center"
        label="二手房"
      >
      </el-table-column>
      <el-table-column
        prop="agentLeasehouse"
        header-align="center"
        align="center"
        label="租房"
      >
      </el-table-column>
      <el-table-column
        prop="agentStatus"
        header-align="center"
        align="center"
        label="状态"
      >
      </el-table-column>
      <el-table-column
        prop="agentImg"
        header-align="center"
        align="center"
        label="经纪人头像"
      >
      </el-table-column>
      <el-table-column
        prop="agentSort"
        header-align="center"
        align="center"
        label="排序"
      >
      </el-table-column>
      <el-table-column
        prop="agentCreateTime"
        header-align="center"
        align="center"
        label="创建时间"
      >
      </el-table-column>
      <el-table-column
        prop="agentUpdateTime"
        header-align="center"
        align="center"
        label="更新时间"
      >
      </el-table-column>
      <el-table-column
        prop="agentDeleteTime"
        header-align="center"
        align="center"
        label="逻辑删除时间"
      >
      </el-table-column>
      <el-table-column
        prop="agentDelete"
        header-align="center"
        align="center"
        label="逻辑删除"
      >
      </el-table-column>
      <el-table-column
        prop="serviceIntroduction"
        header-align="center"
        align="center"
        label="服务介绍"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="updateHandle(scope.row.agentId)"
            >修改</el-button
          >
          <el-button
            :disabled="scope.row.agentStatus != 0 ? true : false"
            type="text"
            size="small"
            @click="open(scope.row.agentId)"
            >启用</el-button
          >
          <el-button
            :disabled="scope.row.agentStatus == 0 ? true : false"
            type="text"
            size="small"
            @click="close(scope.row.agentId)"
            >禁用</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.agentId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[2, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <el-dialog
      :title="!agent.agentId ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible"
    >
      <el-form
        :model="agent"
        :rules="dataRule"
        ref="agent"
        @keyup.enter.native="agentSubmit()"
        label-width="80px"
      >
        <el-form-item label="经纪人名字" prop="agentName">
          <el-input
            v-model="agent.agentName"
            placeholder="经纪人名字"
          ></el-input>
        </el-form-item>
        <el-form-item label="经纪人电话" prop="agentPhone">
          <el-input
            v-model="agent.agentPhone"
            placeholder="经纪人电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="公司id" prop="agentCompany">
          <el-input
            v-model="agent.agentCompany"
            placeholder="公司id"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="agentStatus">
          <el-input v-model="agent.agentStatus" placeholder="状态"></el-input>
        </el-form-item>
        <el-form-item label="经纪人头像" prop="agentImg">
          <el-input
            v-model="agent.agentImg"
            placeholder="经纪人头像"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="agentSort">
          <el-input v-model="agent.agentSort" placeholder="排序"></el-input>
        </el-form-item>
        <el-form-item label="服务介绍" prop="serviceIntroduction">
          <el-input
            v-model="agent.serviceIntroduction"
            placeholder="服务介绍"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="agentSubmit()">确定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :title="!agent.agentId ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible2"
    >
      <el-form
        :model="agent"
        :rules="dataRule"
        ref="agent"
        @keyup.enter.native="agentEdit()"
        label-width="80px"
      >
        <el-form-item label="经纪人Id" prop="agentId">
          <el-input v-model="agent.agentId" placeholder="经纪人id"></el-input>
        </el-form-item>
        <el-form-item label="经纪人名字" prop="agentName">
          <el-input
            v-model="agent.agentName"
            placeholder="经纪人名字"
          ></el-input>
        </el-form-item>
        <el-form-item label="经纪人电话" prop="agentPhone">
          <el-input
            v-model="agent.agentPhone"
            placeholder="经纪人电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="公司id" prop="agentCompany">
          <el-input
            v-model="agent.agentCompany"
            placeholder="公司id"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="agentStatus">
          <el-input v-model="agent.agentStatus" placeholder="状态"></el-input>
        </el-form-item>
        <el-form-item label="经纪人头像" prop="agentImg">
          <el-input
            v-model="agent.agentImg"
            placeholder="经纪人头像"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="agentSort">
          <el-input v-model="agent.agentSort" placeholder="排序"></el-input>
        </el-form-item>
        <el-form-item label="服务介绍" prop="serviceIntroduction">
          <el-input
            v-model="agent.serviceIntroduction"
            placeholder="服务介绍"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible2 = false">取消</el-button>
        <el-button type="primary" @click="agentEdit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 2,
      totalPage: 0,
      dataListLoading: false,
      selectionDataList: [],
      visible: false,
      visible2: false,
      agent: {},
      dataRule: {
        agentName: [
          { required: true, message: "经纪人名字不能为空", trigger: "blur" },
        ],
        agentPhone: [
          { required: true, message: "经纪人电话不能为空", trigger: "blur" },
        ],
        agentCompany: [
          { required: true, message: "公司id不能为空", trigger: "blur" },
        ],
        agentNewhouse: [
          { required: true, message: "新房不能为空", trigger: "blur" },
        ],
        agentOldhouse: [
          { required: true, message: "二手房不能为空", trigger: "blur" },
        ],
        agentLeasehouse: [
          { required: true, message: "租房不能为空", trigger: "blur" },
        ],
        agentStatus: [
          { required: true, message: "状态不能为空", trigger: "blur" },
        ],
        agentImg: [
          { required: true, message: "经纪人头像不能为空", trigger: "blur" },
        ],
        agentSort: [
          { required: true, message: "排序不能为空", trigger: "blur" },
        ],
        agentDeleteTime: [
          { required: true, message: "逻辑删除时间不能为空", trigger: "blur" },
        ],

        serviceIntroduction: [
          { required: true, message: "服务介绍不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    agentSubmit() {
      console.log(this.agent);
      this.visible = false;
      this.$http({
        url: this.$http.adornUrl(`/proprietary/agentuser/save`),
        method: "post",
        data: this.$http.adornData(this.agent, false),
      }).then(({ data }) => {
        console.log(data);
        this.$message({
          message: "操作成功",
          type: "success",
          duration: 1500,
          onClose: () => {
            this.getDataList();
          },
        });
        this.agent={}
      });
    },
    addagent() {
      this.visible = true;
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/proprietary/agentuser/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
        }),
      }).then(({ data }) => {
        console.log(data);

        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    updateHandle(id) {
      this.$http({
        url: this.$http.adornUrl(`/proprietary/agentuser/info/${id}`),
        method: "get",
      }).then(({ data }) => {
        if (data) {
          this.agent = data;
        }
      });
      this.visible2 = true;
    },
    agentEdit() {
      this.$http({
        url: this.$http.adornUrl(`/proprietary/agentuser/update`),
        method: "post",
        data: this.$http.adornData(this.agent, false),
      }).then(({ data }) => {
        console.log(data);
        this.$message({
          message: "操作成功",
          type: "success",
          duration: 1500,
          onClose: () => {
            this.getDataList();
          },
        });
        this.agent = {};
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.agentId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/proprietary/agentuser/delete/${id}`),
          method: "post",
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    open(id) {
      this.agent.agentId = id;
      this.agent.agentStatus = 1;
      this.$http({
        url: this.$http.adornUrl(`/proprietary/agentuser/statusChange`),
        method: "post",
        data: this.$http.adornData(this.agent, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getDataList();
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    close(id) {
      this.agent.agentId = id;
      this.agent.agentStatus = 0;
      this.$http({
        url: this.$http.adornUrl(`/proprietary/agentuser/statusChange`),
        method: "post",
        data: this.$http.adornData(this.agent, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getDataList();
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
  },
};
</script>
